#include <QDebug>
#include "screenwidget.h"
#include "ui_screenwidget.h"
#include "Screen/topmenuwidget.h"
#include "Screen/normalscreen.h"
#include <QVBoxLayout>
#include <QPropertyAnimation>
#include <QPushButton>
#include <QResizeEvent>
#include <Screen/topmenuwidget.h>

ScreenWidget::ScreenWidget(int wid, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ScreenWidget),
    m_isFull(false)
{
    ui->setupUi(this);
//    this->setAttribute(Qt::WidgetAttribute::WA_InputMethodTransparent);

    QVBoxLayout* box = new QVBoxLayout();
    box->setMargin(0);
    m_screen = new NormalScreen(wid);
    m_screen->setFocusPolicy(Qt::FocusPolicy::StrongFocus);
    box->addWidget(m_screen);
    this->setLayout(box);

    m_topMenu = new TopMenuWidget(this);
    m_pPosAnimation1 = new QPropertyAnimation(m_topMenu, "pos");
    m_pPosAnimation1->setDirection(QAbstractAnimation::Backward);
    m_pPosAnimation1->setDuration(500);
    m_pPosAnimation1->setEasingCurve(QEasingCurve::InOutQuad);

    setTopMenuPos(this->width());

    m_timer.setSingleShot(true);
    m_timer.setInterval(3000);
    connect(m_topMenu, SIGNAL(mouseLeave()), &m_timer, SLOT(start()));
    connect(m_topMenu, SIGNAL(mouseEnter()), &m_timer, SLOT(stop()));
    connect(m_topMenu, SIGNAL(mouseEnter()), this, SLOT(menuEnter()));
    connect(m_topMenu, &TopMenuWidget::fullScreen, this, &ScreenWidget::full_slot);
    connect(m_topMenu, &TopMenuWidget::normalScreen, this, &ScreenWidget::normal_slot);
    connect(&m_timer, &QTimer::timeout, this, &ScreenWidget::menuLeave);
}

ScreenWidget::~ScreenWidget()
{
    delete ui;
}

NormalScreen *ScreenWidget::getScreen()
{
    return m_screen;
}

void ScreenWidget::menuEnter()
{
    if(m_pPosAnimation1->direction()==QAbstractAnimation::Forward)
    {
        return;
    }
    m_pPosAnimation1->setDirection(QAbstractAnimation::Forward);
    m_pPosAnimation1->setLoopCount(1);
    m_pPosAnimation1->start();
}

void ScreenWidget::menuLeave()
{
    if(m_pPosAnimation1->direction()==QAbstractAnimation::Backward)
    {
        return;
    }
    m_pPosAnimation1->setDirection(QAbstractAnimation::Backward);
    m_pPosAnimation1->setLoopCount(1);
    m_pPosAnimation1->start();
}

void ScreenWidget::changeEvent(QEvent *event)
{
    if(event->type()==QEvent::WindowStateChange)
    {
        qDebug()<<this->windowState();
        Qt::WindowStates t(this->windowState());
        t.testFlag(Qt::WindowState::WindowMinimized);
        if(t.testFlag(Qt::WindowState::WindowMinimized))
        {
            emit Minimized();
        }
        else
        {
            emit NotMinimized();
        }
    }
}

void ScreenWidget::resizeEvent(QResizeEvent *event)
{
    setTopMenuPos(event->size().width());
}

void ScreenWidget::setTopMenuPos(int parentWidth)
{
    //TODO:Y以后变为 -m_topMenu->height()
    QPoint startPoint = QPoint((parentWidth-m_topMenu->width())/2, -m_topMenu->height()+emitHeight);
    QPoint endPoint = QPoint((parentWidth-m_topMenu->width())/2, 0);
    if(m_pPosAnimation1->direction()==QAbstractAnimation::Backward)
    {
        m_topMenu->move(startPoint);
    }
    else
    {
        m_topMenu->move(endPoint);
    }
    m_pPosAnimation1->setStartValue(startPoint);
    m_pPosAnimation1->setEndValue(endPoint);
}

void ScreenWidget::normal_slot()
{
    this->setWindowState(m_oldWindowState);
}

void ScreenWidget::full_slot()
{
    m_oldWindowState = this->windowState();
    this->showFullScreen();
}
